package com.github_Dididipapa.shortlink.project.service.impl;

import com.github_Dididipapa.shortlink.project.service.AsyncService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

/**
 * @description: To be defined
 * @author: dididi_papa
 * @email: mitis1624087624@gmail.com
 * @date: 2025/10/07  18:47
 */
@Service
@Slf4j
@RequiredArgsConstructor
public class AsyncServiceImpl implements AsyncService {

    private final RedisTemplate<String, String> redisTemplate;

    @Override
    @Async("threadPool")
    public void deleteRedisCache(String key) {
        try {
            redisTemplate.delete(key);
        } catch (Exception e) {
            log.error("异步删除缓存失败:" + e.getMessage());
            throw e;
        }
    }

    @Override
    public void deleteRedisCache(String key, long waitTime) {
        try {
            Thread.sleep(waitTime);
            redisTemplate.delete(key);
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
        } catch (Exception e) {
            log.error("异步删除缓存失败:" + e.getMessage());
            throw e;
        }
    }
}
